﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace MedReg.Classes
{
    [Serializable]
    public class CardRecord : ISerializable
    {
        private decimal durationField;

        private string organisationField;

        private OrganisationType orgTypeField;

        private MedicalCare careField;

        private MedicalCondition conditionsField;

        private Position recrodPositionField;

        private Subdivision recordSubdivisionField;

        private RecordTypeIn typeInField;

        private string orderInField;

        private RecordTypeOut typeOutField;

        private string orderOutField;

        private System.Nullable<System.DateTime> dateBeginField;

        private System.Nullable<System.DateTime> dateEndField;

        private Regime recordRegimeField;

        private string unitField;

        private Military recordMilitaryField;

        private decimal wageField;

        private PositionType recordPositionTypeField;

        private Post recordPostField;

        private Employee employeeField;

        private static readonly CardRecord theOneObject = new CardRecord();

        public static CardRecord GetCardRecord()
        {
            return theOneObject;
        }

        /// <remarks/>
        public decimal Duration
        {
            get
            {
                return this.durationField;
            }
            set
            {
                this.durationField = value;
            }
        }

        /// <remarks/>
        public string Organisation
        {
            get
            {
                return this.organisationField;
            }
            set
            {
                this.organisationField = value;
            }
        }

        /// <remarks/>
        public OrganisationType OrgType
        {
            get
            {
                return this.orgTypeField;
            }
            set
            {
                this.orgTypeField = value;
            }
        }

        /// <remarks/>
        public MedicalCare Care
        {
            get
            {
                return this.careField;
            }
            set
            {
                this.careField = value;
            }
        }

        /// <remarks/>
        public MedicalCondition Conditions
        {
            get
            {
                return this.conditionsField;
            }
            set
            {
                this.conditionsField = value;
            }
        }

        /// <remarks/>
        public Position RecrodPosition
        {
            get
            {
                return this.recrodPositionField;
            }
            set
            {
                this.recrodPositionField = value;
            }
        }

        /// <remarks/>
        public Subdivision RecordSubdivision
        {
            get
            {
                return this.recordSubdivisionField;
            }
            set
            {
                this.recordSubdivisionField = value;
            }
        }

        /// <remarks/>
        public RecordTypeIn TypeIn
        {
            get
            {
                return this.typeInField;
            }
            set
            {
                this.typeInField = value;
            }
        }

        /// <remarks/>
        public string OrderIn
        {
            get
            {
                return this.orderInField;
            }
            set
            {
                this.orderInField = value;
            }
        }

        /// <remarks/>
        public RecordTypeOut TypeOut
        {
            get
            {
                return this.typeOutField;
            }
            set
            {
                this.typeOutField = value;
            }
        }

        /// <remarks/>
        public string OrderOut
        {
            get
            {
                return this.orderOutField;
            }
            set
            {
                this.orderOutField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<System.DateTime> DateBegin
        {
            get
            {
                return this.dateBeginField;
            }
            set
            {
                this.dateBeginField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<System.DateTime> DateEnd
        {
            get
            {
                return this.dateEndField;
            }
            set
            {
                this.dateEndField = value;
            }
        }

        /// <remarks/>
        public Regime RecordRegime
        {
            get
            {
                return this.recordRegimeField;
            }
            set
            {
                this.recordRegimeField = value;
            }
        }

        /// <remarks/>
        public string Unit
        {
            get
            {
                return this.unitField;
            }
            set
            {
                this.unitField = value;
            }
        }

        /// <remarks/>
        public Military RecordMilitary
        {
            get
            {
                return this.recordMilitaryField;
            }
            set
            {
                this.recordMilitaryField = value;
            }
        }

        /// <remarks/>
        public decimal Wage
        {
            get
            {
                return this.wageField;
            }
            set
            {
                this.wageField = value;
            }
        }

        /// <remarks/>
        public PositionType RecordPositionType
        {
            get
            {
                return this.recordPositionTypeField;
            }
            set
            {
                this.recordPositionTypeField = value;
            }
        }

        /// <remarks/>
        public Post RecordPost
        {
            get
            {
                return this.recordPostField;
            }
            set
            {
                this.recordPostField = value;
            }
        }

        public Employee Employee
        {
            get
            {
                return employeeField;
            }
            set
            {
                employeeField = value;
            }

        }

     
        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
           // throw new NotImplementedException();
            info.AddValue("Duration", durationField);
            info.AddValue("Organisation", organisationField);
            info.AddValue("OrgType", orgTypeField);
            info.AddValue("Care", careField);
            info.AddValue("Conditions", conditionsField);
            info.AddValue("RecrodPosition", recrodPositionField);
            info.AddValue("RecordSubdivision", recordSubdivisionField);
            info.AddValue("TypeIn", typeInField);
            info.AddValue("OrderIn", orderInField);
            info.AddValue("TypeOut", typeOutField);
            info.AddValue("OrderOut", orderOutField);
            info.AddValue("DateBegin", dateBeginField);
            info.AddValue("DateEnd", dateEndField);
            info.AddValue("RecordRegime", recordRegimeField);
            info.AddValue("Unit", unitField);
            info.AddValue("RecordMilitary", recordMilitaryField);
            info.AddValue("Wage", wageField);
            info.AddValue("RecordPositionType", recordPositionTypeField);
            info.AddValue("RecordPost", recordPostField);

        }
        public CardRecord ()
        {
        }

        public List<Classes.CardRecord> getCardRecord(Classes.ArrayOfEmployee sEmployee)
        {

            foreach (var emp in sEmployee)
            {
                foreach (var cardRecord in emp.EmployeeRecords)
                {
                    cardRecord.Employee = emp;
                }
            }

            //var awd = sEmployee
            //    .SelectMany(award => award.EmployeeAwards, (Employees, Awards) => new { Employees, Awards })
            //    .Select(AwardsEx => new { Employee = AwardsEx.Employees, AwardsEx.Awards }).ToList();

            var cr = sEmployee.SelectMany(em => em.EmployeeRecords).ToList();

            return cr;
        }
    }
}
